package com.ssm.pojo;

import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import java.io.File;
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * -----------------------------------------
 * 文件: Email.java
 * 邮箱: fengemail04@sina.com
 * 描述: 发送Email工具类
 * -----------------------------------------
 */
public class Email {
    private static final Logger logger = LoggerFactory.getLogger(Email.class);

    private MailSender mailSender;

    private SimpleMailMessage simpleMailMessage;
    @Resource
    private JavaMailSender myMails;

    /**
     * 方法名: sendMail
     * 参数名：@param subject  邮件主题
     * 参数名：@param content 邮件主题内容
     * 参数名：@param to      收件人Email地址
     * 描述语: 发送邮件
     */
    public void sendMail(String subject, String content, String to) {

        simpleMailMessage.setSubject(subject); //设置邮件主题
        simpleMailMessage.setTo(to);             //设定收件人
        simpleMailMessage.setText(content);  //设置邮件主题内容
        mailSender.send(simpleMailMessage); //发送邮件
    }
    //Spring 依赖注入
    public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
        this.simpleMailMessage = simpleMailMessage;
    }
    //Spring 依赖注入
    public void setMailSender(MailSender mailSender) {

        this.mailSender = mailSender;
    }

    /**
     *
     * @Description:普通文本发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * void
     * @exception:
     */
    public void sendSimpleMail(String subject,String content,String toMail) {
        simpleMailMessage.setSubject(subject);
        simpleMailMessage.setText(content);
        simpleMailMessage.setTo(toMail);
        mailSender.send(simpleMailMessage);
        logger.info("邮件发送成功..");
    }

    /**
     *
     * @Description:html发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * void
     * @exception:
     */
    public void sendHtmlMail(String subject,String content,String toMail) {
        MimeMessage mailMessage = myMails.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage);
        try {
            messageHelper.setTo(toMail);
            messageHelper.setSubject(subject);
            messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true);
            myMails.send(mailMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @Description: 带图片发邮件形式
     * @param subject 主题
     * @param content 正文
     * @param toMail 收件人邮箱
     * @param picturePath 图片路径
     * void
     * @exception:
     */
    public void sendPictureMail(String subject,String content,String toMail,String picturePath){
        MimeMessage mailMessage = myMails.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true);
            messageHelper.setTo(toMail);
            messageHelper.setSubject(subject);
            //messageHelper.setText("<html><head></head><body><h1>" + content +"</h1><img src=/"+cid:aaa + /"/></body></html>",true);
            FileSystemResource img = new FileSystemResource(new File(picturePath));
            messageHelper.addInline("aaa",img);
            //发送邮件
            myMails.send(mailMessage);

        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * @Description:带附件发邮件形式
     * @param subject 标题
     * @param content 正文
     * @param toMail 收件人邮箱
     * @param accessoryPath 附件路径
     * @param accessoryName 附件名
     * void
     * @exception:
     */
    public void sendMailTakeAccessory(String subject,String content,String toMail,String accessoryPath,String accessoryName){
        MimeMessage mailMessage = myMails.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(mailMessage,true,"utf-8");
            messageHelper.setTo(toMail);
            messageHelper.setSubject(subject);
            messageHelper.setText("<html><head></head><body><h1>"+content+"</h1></body></html>",true);
            FileSystemResource file = new FileSystemResource(new File(accessoryPath));
            messageHelper.addAttachment(accessoryName,file);
            myMails.send(mailMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }

    }


}